﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace AutoCadConvert
{
    class Enter : BspObject
    {
        public List<int> m_AreaRef = new List<int>();
        public Point m_Pt = new Point();
        public IEnumerable<XElement> ChildXml()
        {
            foreach (int item in m_AreaRef)
                yield return new XElement("_" + m_AreaRef.IndexOf(item), item);
        }
        public override IEnumerable<XElement> ToXml()
        {
            return base.ToXml().Concat(new XElement[] {
                new XElement("Areas",ChildXml()),
                new XElement("Pos",m_Pt.ToMirror().To3DString()),
            });
        }
    }
    class Entery:Collection
    {
        List<Enter> m_Entery = new List<Enter>();
        public Entery(House house):base(house)
        {
        }
        public override void AjustLineAndPoint()
        {
            List<Area> area = m_house.Item<Areas>().m_Areas;
            //门
            List<Door> door = m_house.Item<Doors>().m_Door;
            foreach (Door dr in door)
            {
                IEnumerable<Area> lst = area.Where(o => Area.isInBoundary(o.m_LogicPoints, dr.Pos));
                if (lst.Count() > 2)    lst = lst.Take(2);
                m_Entery.Add(new Enter() { m_AreaRef = lst.Select(o => area.IndexOf(o)).ToList(), m_Pt = new Point(dr.Pos) });
            }
            //雅口
            List<Pass> pass = m_house.Item<Passes>().m_Pass;
            foreach (Pass ps in pass)
            {
                IEnumerable<Area> lst = area.Where(o => Area.isInBoundary(o.m_LogicPoints, ps.Pos));
                if (lst.Count() > 2) lst = lst.Take(2);
                m_Entery.Add(new Enter() { m_AreaRef = lst.Select(o => area.IndexOf(o)).ToList(), m_Pt = new Point(ps.Pos) });
            }
            //区域线
            List<Line> line = m_house.Item<Lines>().m_Lines.Where(o=>o.IsLogicLine).ToList();
            foreach (Line ln in line)
            {
                IEnumerable<Area> lst = area.Where(o => Area.isInBoundary(o.m_LogicPoints, ln.Middle));
                if (lst.Count() > 2) lst = lst.Take(2);
                m_Entery.Add(new Enter() { m_AreaRef = lst.Select(o => area.IndexOf(o)).ToList(), m_Pt = new Point(ln.Middle) });
            }
        }
        public override IEnumerable<XElement> ToXml()
        {
            int idx = 0;
            foreach (Enter ey in m_Entery)
                yield return new XElement("_" + idx++, ey.ToXml());
        }
    }
}
